namespace LightBox.Setup
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Configuration.Install;

    using Microsoft.Win32;

    using LightBox.Properties;

    [RunInstaller(true)]
    public partial class StudioReferenceInstaller : Installer
    {
        public StudioReferenceInstaller()
        {
            InitializeComponent();
        }

        public string InstallPath
        {
            get
            {
                return Context.Parameters.ContainsKey("LBPATH") ? Context.Parameters["LBPATH"] : null;
            }
        }

        public override void Install(System.Collections.IDictionary stateSaver)
        {
            RegistryReference(false);
        }

        public override void Uninstall(System.Collections.IDictionary savedState)
        {
            RegistryReference(true);
        }

        private void RegistryReference(bool remove)
        {
            RegistryKey asmKey = GetAssemblyFoldersKey();

            if (asmKey != null)
            {
                try
                {
                    if (!remove)
                    {
                        RegistryKey key = asmKey.CreateSubKey("LightBox.NET");
                        key.SetValue(null, InstallPath);
                        key.Close();
                    }
                    else
                    {
                        asmKey.DeleteSubKey("LightBox.NET");
                    }
                }
                finally
                {
                    asmKey.Close();
                }
            }
        }

        private RegistryKey GetAssemblyFoldersKey()
        {
            return Registry.LocalMachine.OpenSubKey(Resources.SubKeyName, true);
        }

    }
}